//
//  MaterialViewController.m
//  A手工客
//
//  Created by 萌萌的小坤坤 on 16/6/13.
//  Copyright © 2016年 冷坤. All rights reserved.
//

#import "MaterialViewController.h"
#import "HeadViewTwo_material.h"
#import "OneTabViewCell_material.h"
#import "TwoTabViewCell_material.h"
#import "ThreeTableViewCell_material.h"
#import "AFNetworking.h"
#import "ProductModel.h"
#import "SectionViewOne.h"
#import "SectionViewTwo.h"
#define MATERIALURL @"http://m.shougongke.com/index.php?c=Shiji&vid=18"
#define WIDTH self.view.frame.size.width

#define HEIGHT self.view.frame.size.height

#define COLORS [UIColor colorWithRed:(arc4random() % 256 / 255.0) green:(arc4random() % 256 / 255.0) blue:(arc4random() % 256 / 255.0) alpha:1.0];

@interface MaterialViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic ,retain) UITableView *tabView;
@property (nonatomic, retain) ProductModel *model;

@end

@implementation MaterialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTabView];
    [self getProductURL];
}


- (void)getProductURL{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil];
    [manager GET:MATERIALURL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *data = responseObject[@"data"];
        self.model = [ProductModel productModelWithDic:data];
        [self creatHeadCollection];
        [self.tabView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}



- (void)creatHeadCollection{
    HeadViewTwo_material *headCollection = [[HeadViewTwo_material alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];;
    headCollection.dataSource = self.model;

    headCollection.backgroundColor = [UIColor greenColor];
    self.tabView.tableHeaderView = headCollection;
    
}
- (void)creatTabView{
    self.tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 100) style:1];
    self.tabView.dataSource = self;
    self.tabView.delegate = self;
    
    [self.view addSubview:self.tabView];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString *OneID = @"OneCell";
        OneTabViewCell_material *oneCell = [tableView dequeueReusableCellWithIdentifier:OneID];
        if (oneCell == nil) {
            oneCell = [[OneTabViewCell_material alloc]initWithStyle:1 reuseIdentifier:OneID];
        }
        oneCell.dataSource = self.model.best;
        return oneCell;
    }else if (indexPath.section == 1){
        static NSString *TwoID = @"TwoCell";
        TwoTabViewCell_material *twoCell = [tableView dequeueReusableCellWithIdentifier:TwoID];
        if (twoCell == nil) {
            twoCell = [[TwoTabViewCell_material alloc]initWithStyle:1 reuseIdentifier:TwoID];
        }
        twoCell.dataSource = self.model.topicBest;
        return twoCell;
    }else{
        static NSString *ThreeID = @"ThreeCell";
        ThreeTableViewCell_material *threeCell = [tableView dequeueReusableCellWithIdentifier:ThreeID];
        if (threeCell == nil) {
            threeCell = [[ThreeTableViewCell_material alloc]initWithStyle:1 reuseIdentifier:ThreeID];
        }
        threeCell.models = self.model.topic[indexPath.row];
        return threeCell;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 1;
    }else{
        return self.model.topic.count;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 200;
    }else if (indexPath.section == 1){
        return 130;
    }else{
        return 200;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0000000000000000000000000000001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        SectionViewOne *sectionOne = [[SectionViewOne alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
        return sectionOne;
    }else if (section == 1){
        SectionViewTwo *sectionTwo = [[SectionViewTwo alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
        return sectionTwo;
    }else{
        return nil;
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
